<?php

namespace app\service;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use app\exception\ParameterException;
use Couchbase\PreparedStatementException;

class AliSms
{


    public function sendSms($mobile,$code,$mark){



        AlibabaCloud::accessKeyClient(env("sms.accessKeyId"), env("sms.accessSecret"))
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product("ShortMessage")
                ->version("2017-05-25")
                ->action("SendSms")
                ->method("POST")
                ->host("dysmsapi.aliyuncs.com")
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $mobile,
                        'TemplateCode' => env("sms.templateCode"),
                        'SignName' => env("sms.signName"),
                        'TemplateParam' => '{"code":"'.$code.'"}'
                    ],
                ])
                ->request();

            $res = $result->toArray();

            if ($res['Message'] !=='OK' || $res['Code'] !== 'OK'){
                throw new ParameterException($res['Message']);

            }

            return true;

        } catch (ClientException $e) {
            throw new ParameterException($e->getErrorMessage());
//            $this->_error[]=$e->getErrorMessage() . PHP_EOL;
//            return false;
        } catch (ServerException $e) {
            throw new ParameterException($e->getErrorMessage());
//            $this->_error[]=$e->getErrorMessage() . PHP_EOL;
//            return false;
        }

    }
}
